package com.hxzy.controller.admin.employee;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.RCodeEnum;
import com.hxzy.common.exception.MarketException;
import com.hxzy.common.resp.R;
import com.hxzy.controller.admin.employee.req.EmployeeAddReq;
import com.hxzy.controller.admin.employee.req.EmployeeEditPwdReq;
import com.hxzy.controller.admin.employee.req.EmployeeEditReq;
import com.hxzy.controller.admin.employee.req.EmployeeSearchReq;
import com.hxzy.controller.admin.employee.resp.EmployeeEditResp;
import com.hxzy.controller.admin.employee.resp.EmployeeResp;
import com.hxzy.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 功能描述
 *
 * @author tonneyyy
 */
@Api(tags = "后台-员工API")
@RestController
@RequestMapping(value = "/api/employee")
@SaCheckLogin
public class EmployeeController {

    /**
     * 注入员工表
     */
    @Autowired
    private EmployeeService employeeService;

    /**
     * 分页查询
     * @param req
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R  search(@Valid EmployeeSearchReq req){
        PageInfo<EmployeeResp> pg=this.employeeService.search(req);
        return R.success(pg);
    }


    /**
     * 新增员工
     * @param req
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增员工")
    public R add(@RequestBody @Valid EmployeeAddReq req){
        boolean result=this.employeeService.add(req);
        return result?R.success():R.fail(RCodeEnum.DATABASE_ERROR);
    }

    @ApiOperation(value = "查询账号是否存在")
    @GetMapping(value = "/account/exists/{account}")
    public R accountExist(@PathVariable("account") String account){
        boolean result= this.employeeService.accountExist(account);
        return result?R.success( RCodeEnum.ACCOUNT_EXIST.getMessage()):R.success();
    }


    @ApiOperation(value = "根据ID查询员工")
    @GetMapping(value = "/{id}")
    public R findById(@PathVariable(value = "id") Long id){
        EmployeeEditResp resp=this.employeeService.findById(id);
        return R.success(resp);
    }

    @ApiOperation(value = "修改员工信息")
    @PutMapping
    public R update(@RequestBody @Valid EmployeeEditReq req){
       boolean result= this.employeeService.edit(req);
        return result? R.success(): R.fail(RCodeEnum.DATABASE_UPDATE_ERROR);
    }

    @ApiOperation(value = "修改员工信息及密码")
    @PutMapping(value = "/update")
    public R updatePwd(@RequestBody @Valid EmployeeEditPwdReq req){
        //权限认证
       if("0".equals(StpUtil.getExtra("root"))){
           throw new MarketException(RCodeEnum.PERMISSION_DENIED);
        }
        boolean result= this.employeeService.edit(req);
        return result? R.success(): R.fail(RCodeEnum.DATABASE_UPDATE_ERROR);
    }

}
